package com.hzw.code

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.hzw.code.api.Api
import com.hzw.code.api.UpdateApi
import com.hzw.code.api.WorkspaceApi
import com.hzw.code.extend.convertJsonRequestBody
import com.hzw.code.extend.startActivity
import com.hzw.code.net.HttpManager
import com.safframework.log.L
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val api = HttpManager.getApi<Api>(Api::class.java)

        getServerTime.setOnClickListener {
            lifecycleScope.launch {
                val serverTime = api.getServerTime()
                L.i("globalInfo = $serverTime")
            }
        }

        getGlobalInfo.setOnClickListener {
            lifecycleScope.launch {
                val globalInfo = api.getGlobalInfo()
                L.i("globalInfo = $globalInfo")
            }
        }

        fetchPatientDetail.setOnClickListener {
            lifecycleScope.launch {
                val fetchPatientDetail =
                    api.fetchPatientDetail(convertJsonRequestBody("patientUnionId" to "3878528916247760735"))
                L.i("fetchPatientDetail = $fetchPatientDetail")
            }
        }

        checkUpdate.setOnClickListener {
            val updateApi = HttpManager.getApi<UpdateApi>(
                UpdateApi::class.java
            )
            lifecycleScope.launch {
                val checkUpdate = updateApi.checkUpdate(2, 1)
                L.i("checkUpdate = $checkUpdate")
            }
        }

        fetchWorkspace.setOnClickListener {
            val workspaceApi = HttpManager.getApi<WorkspaceApi>(
                WorkspaceApi::class.java
            )
            lifecycleScope.launch {
                val workspace =
                    workspaceApi.loadConsultationList(
                        convertJsonRequestBody(
                            "consultationStatus" to 2,
                            "page" to 1,
                            "limit" to 20
                        )
                    )
                L.i("workspace = $workspace")
            }
        }

        goToPageStateActivity.setOnClickListener {
            startActivity<PageStateActivity>()
        }

        testPageActivity.setOnClickListener {
            startActivity<NormalActivity>()
        }

    }
}
